// pages/personnal/addAddress/addAddress.js
const app = getApp();
const util = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: '请选择收货时间',
    region: ['广东省', '广州市', '海珠区'],
    customItem: '全部',
    status: 0
  },

  /**
   * 保存
   */
  addAddress:function(){
    let name = this.data.name
    let phone = this.data.phone
    let region = this.data.region
    let address = this.data.address
    if(name==""||name==null){
      wx.showToast({
        title: '姓名不能为空',
        icon: 'loading',
        duration: 2000
      });
      return;
    } if (phone == "" || phone == null) {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'loading',
        duration: 2000
      });
      return;
    } if (address == "" || address == null) {
      wx.showToast({
        title: '地址不能为空',
        icon: 'loading',
        duration: 2000
      });
      return;
    }

    let getTitleDetailUrl = app.globalData.baseUrl + "addAddress"
    let data = {
      cid: wx.getStorageSync('cid'),
      receiveName: name,
      receiveCellphone: phone,
      location: region,
      address: address
    }
    util.requestLoading(getTitleDetailUrl, data, "GET").then(res => {
      if (res.data.status==0){
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        });
        setTimeout(function () {
          wx.navigateBack({ changed: true })
        }, 2000)
      }else{
        wx.showToast({
          title: '保存失败',
          icon: 'loading',
          duration: 2000
        });
      }
    })
  },

  /**
   * 编辑
   */
  editAddress: function () {
    let that = this
    let name = this.data.name
    let phone = this.data.phone
    let region = this.data.region
    let address = this.data.address
    if (name == "" || name == null) {
      wx.showToast({
        title: '姓名不能为空',
        icon: 'loading',
        duration: 2000
      });
      return;
    } if (phone == "" || phone == null) {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'loading',
        duration: 2000
      });
      return;
    } if (address == "" || address == null) {
      wx.showToast({
        title: '地址不能为空',
        icon: 'loading',
        duration: 2000
      });
      return;
    }

    let getTitleDetailUrl = app.globalData.baseUrl + "editAddress"
    let data = {
      addressId: this.data.addressId,
      receiveName: name,
      receiveCellphone: phone,
      location: region,
      address: address
    }
    util.requestLoading(getTitleDetailUrl, data, "GET").then(res => {
      if (res.data.status == 0) {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        });
        setTimeout(function () {
          wx.navigateBack({ changed: true })
        }, 2000)
      } else {
        wx.showToast({
          title: '保存失败',
          icon: 'loading',
          duration: 2000
        });
      }
    })
  },

  name:function(event){
    this.setData({
      name: event.detail.value
    })
  },

  phone: function (event) {
    this.setData({
      phone: event.detail.value
    })
  },

  address: function (event) {
    this.setData({
      address: event.detail.value
    })
  },

  bindTimeChange(e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },
  bindRegionChange(e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.address!=null){
      let address = JSON.parse(options.address)
      let region = address.location.replace("[", "").replace("]", "").replace(/\"/g, "")
      // console.log(region)
      this.setData({
        addressId: address.addressId,
        status:1,
        name: address.receiveName,
        phone: address.receiveCellphone,
        region: region,
        address: address.address
      })
    }
   
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})